local LUF = LibStub("AceAddon-3.0"):GetAddon("lUnitFrame")
local db
local options 


local MODNAME = "HPMP"
local mod = LUF:NewModule(MODNAME, "AceEvent-3.0")


local numberize = function(v)
	if v <= 9999 then return v end
	if v >= 1000000 then
		local value = string.format("%.1fm", v/1000000)
		return value
	elseif v >= 10000 then
		local value = string.format("%.1fk", v/1000)
		return value
	end
end

local function HideMe(fontString)
	fontString:Hide()
end

local function getOptions()
	if not options then
		options ={
			type = "group",
			name  = MODNAME,
			args ={
				player={
					type = "toggle",
					name = "Player",
					order = 10,
					get = function() return db["player"].enable end,
					set = function(info, v)
						db["player"].enable = v 
						mod:ConfigString("player")end;
					},
				target = {
					type = "toggle",
					name = "Target",
					order = 20,
					get = function() return db["target"].enable end,
					set = function(info,v)
						db["target"].enable = v 
						mod:ConfigString("target")end;
					
					},
					focus = {
					type = "toggle",
					name = "focus",
					order = 30,
					get = function() return db["focus"].enable end,
					set = function(info,v)
						db["focus"].enable = v 
						mod:ConfigString("focus")end;
					
					},
						
						
				}
		}
	end
	return options
end

function mod:OnInitialize()
	self.db = LUF.db:RegisterNamespace(MODNAME)
    self.db:RegisterDefaults({
        profile = {
			["player"] = {enable= true,x=0,y=0,},
			["target"] = {enable = true,x=0,y=0},
			["focus"]  = {enable = true,x=0,y=0},
			["party1"] = {enable =true,x=70,y=0},
			["party2"] = {enable =true,x=70,y=0},
			["party3"] = {enable =true,x=70,y=0},
			["party4"] = {enable =true,x=70,y=0},
		}
	}
	)
	db = self.db.profile
	LUF:RegisterModuleOptions(MODNAME, getOptions)
end



function mod:OnEnable()
	LUF.Print(MODNAME.." is loaded")
    self:RegisterEvent("UNIT_MANA");
	self:RegisterEvent("UNIT_HEALTH");
	self:RegisterEvent("PLAYER_FOCUS_CHANGED","UpdateUnit");
	self:RegisterEvent("PARTY_MEMBERS_CHANGED","UpdateUnit");
	self:RegisterEvent("PLAYER_TARGET_CHANGED","UpdateUnit")
	for k,_ in pairs(LUF.frames) do
		self:CreateUnitString(k);
		self:ConfigString(k);
	end
	self:UpdateUnit()
end

function mod:CreateUnitString(unit)
local frame= LUF.frames[unit];
local hpmpframe = CreateFrame("frame",nil,frame)
local hp=hpmpframe:CreateFontString(nil,"OVERLAY",'GameFontHighlightSmall');
local mp=hpmpframe:CreateFontString(nil,"OVERLAY",'GameFontHighlightSmall');
frame.hp=hp;
frame.mp=mp;
frame.hpmpframe = hpmpframe
hpmpframe:SetFrameLevel(frame:GetFrameLevel()+2)
hooksecurefunc(_G[frame:GetName().."HealthBar"].TextString,"Show",HideMe);
hooksecurefunc(_G[frame:GetName().."ManaBar"].TextString,"Show",HideMe);

end

function mod:ConfigString(unit)
	local frame = LUF.frames[unit]
	frame.hp:SetPoint("CENTER",frame:GetName().."HealthBar","CENTER",db[unit].x,db[unit].y);
	frame.mp:SetPoint("CENTER",frame:GetName().."ManaBar","CENTER",db[unit].x,db[unit].y);
	if db[unit].enable then
		frame.hp:Show();
		frame.mp:Show();
	else
		frame.hp:Hide();
		frame.mp:Hide();
	end
end

function mod:UpdateUnit()
	--if arg1 and arg1 == "player" or arg1 == "target" or string.find(arg1, "^party%d$") then
	for k , _ in pairs(LUF.frames) do
	self:UpdateHP(k);
	self:UpdateMP(k)
	end
	--end
end



function mod:PLAYER_FOCUS_CHANGED()
	self:UpdateHP("focus");
	self:UpdateMP("focus");
end

function mod:PLAYER_TARGET_CHANGED()
	self:UpdateHP("target");
	self:UpdateMP("target");
end

function mod:UNIT_HEALTH()
	if arg1 == "player" or arg1 == "target" or string.find(arg1, "^party%d$") then
		self:UpdateHP(arg1)
	end
end

function mod:UNIT_MANA()
	if arg1 == "player" or arg1 == "target" or string.find(arg1, "^party%d$") then
		self:UpdateMP(arg1)
	end

end

function mod:PARTY_MEMBERS_CHANGED()
	mod:UpdateHP(arg1);
	mod:UpdateMP(arg1)
		

end

--[[ function mod:UNIT_RAGE()
	if arg1 == "player" or arg1 == "target" then
		self:UpdateMP(arg1)
	end

end

function mod:UNIT_ENERGY()
	if arg1 == "player" or arg1 == "target" then
		self:UpdateMP(arg1)
	end

end ]]--

function mod:UpdateHP(unit)
	if UnitIsDead(unit) or not UnitIsConnected(unit) then 
		LUF.frames[unit].hp:SetText("");
		--LUF.frames[unit].mp:SetText("");
		return
	end
	local currValue = UnitHealth(unit);
	local maxValue = UnitHealthMax(unit);
	--LUF:Print(currValue..":"..maxValue)
	if maxValue == 0 then
		LUF.frames[unit].hp:SetText("-");
	else
		--LUF.frames[unit].hp:SetText(currValue.."/"..maxValue)
		LUF.frames[unit].hp:SetText(numberize(currValue).."/"..numberize(maxValue))
	end
end

function mod:UpdateMP(unit)
	if UnitIsDead(unit) or not UnitIsConnected(unit) then 
		--LUF.frames[unit].hp:SetText("");
		LUF.frames[unit].mp:SetText("");
		return
	end
	local currValue = UnitMana(unit);
	local maxValue = UnitManaMax(unit);
	--LUF:Print(currValue..":"..maxValue)
	if maxValue == 0 then
		LUF.frames[unit].mp:SetText("-");
	else
		LUF.frames[unit].mp:SetText(currValue.."/"..maxValue)
	end
end
	


